package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult7;
import com.itheima.common.Simple;
import com.itheima.entity.*;
import com.itheima.mapper.SubjectsDirectorysMapper;
import com.itheima.mapper.SubjectsMapper;
import com.itheima.mapper.SubjectsTagsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.service.SubjectsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cfr
 * @date 2022/1/9
 * @TIME:21:10
 */
@Service
public class SubjectsServiceImpl extends ServiceImpl<SubjectsMapper, Subjects> implements SubjectsService {
    @Autowired
    private SubjectsMapper subjectsMapper;
    @Autowired
    private SubjectsTagsMapper subjectsTagsMapper;
    @Autowired
    private SubjectsDirectorysMapper subjectsDirectorysMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public String modify(Subjects subjects) {
        //通过id修改学科
        Subjects oldSubjects = subjectsMapper.selectById(subjects.getId());
        oldSubjects.setSubjectName(subjects.getSubjectName());
        oldSubjects.setIsFrontDisplay(subjects.getIsFrontDisplay());
        subjectsMapper.updateById(oldSubjects);
        return "{\"success\": \" true\"}";
    }

    @Override
    public String deleteById(SubjectsDto subjectsDto) {
        //查有无标签,有不删
        LambdaQueryWrapper<SubjectsTags> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(SubjectsTags::getSubjectID,subjectsDto.getId()  );
        List<SubjectsTags> subjectsTags = subjectsTagsMapper.selectList(wrapper1);
        if(subjectsTags.size()>0){
            return "{\"code\": 1,\"error\": \" false\"}";

        }

        //查有无目录,有不删
        LambdaQueryWrapper<SubjectsDirectorys> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(SubjectsDirectorys::getSubjectID,subjectsDto.getId()  );
        List<SubjectsDirectorys> subjectsDirectorys = subjectsDirectorysMapper.selectList(wrapper2);
        if(subjectsDirectorys.size()>0){
            return "{\"code\": 1,\"error\": \" false\"}";
        }
        //删除此学科
        subjectsMapper.deleteById(subjectsDto.getId());
        return "{\"success\": \" true\"}";
    }

    @Override
    public String add(Subjects subjects) {
        //补齐数据,添加学科
        subjects.setAddDate(new Date());
        subjects.setTags(0);
        subjects.setTwoLevelDirectory(0);
        subjects.setTotals(0);
        subjectsMapper.insert(subjects);
        return "{id: "+subjects.getId()+"}";
    }

    @Override
    public Simple[] simple() {
        //添加学科id和名字
        List<Subjects> subjects = subjectsMapper.selectList(null);
        Simple[]simples=new Simple[subjects.size()];
        int i=0;
        for (Subjects subject : subjects) {
            Simple simple = new Simple();
            simple.setValue(subject.getId());
            simple.setLabel(subject.getSubjectName());
            simples[i]=simple;
            i++;
        }
        return simples;
    }

    @Override
    public PageResult7 finfPage(Long page, Long pagesize, String subjectName) {
        //分页查询
        IPage<Subjects> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<Subjects> wrapper = new LambdaQueryWrapper<>();
        if (subjectName!= null) {
            wrapper.like(Subjects::getSubjectName,subjectName);
        }
        IPage<Subjects> subjectsTagsIPage = subjectsMapper.selectPage(p, wrapper);
        String s = JSON.toJSONString(subjectsTagsIPage.getRecords());
        List<SubjectsDto> subjectsDtos = JSON.parseArray(s, SubjectsDto.class);
        for (SubjectsDto subjectsDto : subjectsDtos) {
            subjectsDto.setUsername(userMapper.selectById(subjectsDto.getCreatorID()).getUsername());
        }
        //赛值
        PageResult7 pageResult7 = new PageResult7();
        pageResult7.setCounts(subjectsTagsIPage.getTotal());
        pageResult7.setPage(String.valueOf(page));
        pageResult7.setPagesize(String.valueOf(pagesize));
        pageResult7.setPages(p.getPages());
        pageResult7.setItems(subjectsDtos);
        return pageResult7;
    }

    @Override
    public SubjectsDto findById(Integer id) {
        Subjects subjects = subjectsMapper.selectById(id);
        SubjectsDto subjectsDto = new SubjectsDto();
        BeanUtils.copyProperties(subjects, subjectsDto);
        subjectsDto.setCreator(userMapper.selectById(subjectsDto.getCreatorID()).getUsername());
        return subjectsDto;
    }
}
